<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名称" prop="productname">
        <el-input
          v-model="queryParams.productname"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="品类" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择分类" clearable style="width:100%">
          <el-option
            v-for="item in categoryOption"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="标识" prop="productflag">
        <el-select v-model="queryParams.productflag" placeholder="请选择标识" clearable size="small">
          <el-option
            v-for="dict in productflagOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:productinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:productinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:productinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:productinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名称" align="center" prop="productname" />
      <el-table-column label="商品说明" align="center" prop="remark" />
      <el-table-column label="品类" align="center" prop="categoryClass.name" />
      <el-table-column label="税率" align="center" prop="tax" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="创建时间" align="center" prop="createdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="lastupdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastupdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="标识" align="center" prop="productflag" :formatter="productflagFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:productinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:productinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名称" prop="productname">
          <el-input v-model="form.productname" placeholder="请输入商品名称" />
        </el-form-item>
        <el-form-item label="商品说明" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入商品说明" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品类" prop="category">
          <el-select v-model="form.category" placeholder="请选择分类" clearable style="width:100%">
            <el-option
              v-for="item in categoryOption"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="标识" prop="productflag">
          <el-select v-model="form.productflag" multiple placeholder="请选择标识">
            <el-option
              v-for="dict in productflagOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="系列" prop="series">
          <el-select v-model="form.series"  placeholder="请选择系列">
            <el-option
              v-for="dict in productSeries"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="税率" prop="tax">
          <el-input v-model="form.tax" placeholder="请输入税率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listProductinfo, getProductinfo, delProductinfo, addProductinfo, updateProductinfo, exportProductinfo } from "@/api/product/productinfo";
  import { findCategoryList } from "@/api/public";

  export default {
    name: "Productinfo",
    components: {
    },
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 商品管理表格数据
        productinfoList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 状态字典
        statusOptions: [],
        // 标识字典
        productflagOptions: [],
        //系列字典
        productSeries: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          productname: null,
          status: null,
          category: null,
          productflag: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          productname: [
            { required: true, message: "商品名称不能为空", trigger: "blur" }
          ],
          status: [
            { required: true, message: "状态不能为空", trigger: "change" }
          ],
          productflag: [
            { required: true, message: "标识不能为空", trigger: "blur" }
          ]
        },
        categoryOption: []
      };
    },
    created() {
      this.getList();
      this.findCategoryList();
      this.getDicts("general_status").then(response => {
        this.statusOptions = response.data;
      });
      this.getDicts("product_flag").then(response => {
        this.productflagOptions = response.data;
      });
      this.getDicts("product_series").then(response => {
        this.productSeries = response.data;
      });
    },
    methods: {
      //获取分类列表
      findCategoryList(){
        findCategoryList().then(response => {
          this.categoryOption = response.data;
        });
      },
      /** 查询商品管理列表 */
      getList() {
        this.loading = true;
        listProductinfo(this.queryParams).then(response => {
          this.productinfoList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 状态字典翻译
      statusFormat(row, column) {
        return this.selectDictLabel(this.statusOptions, row.status);
      },
      // 标识字典翻译
      productflagFormat(row, column) {
        return this.selectDictLabel(this.productflagOptions, row.productflag);
      },
      // 标识字典翻译
      productSeries(row, column) {
        return this.selectDictLabel(this.productSeries, row.series);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          guid: null,
          productname: null,
          remark: null,
          status: null,
          createdate: null,
          operatorid: null,
          lastupdate: null,
          category: null,
          productflag: null,
          series: null,
          tax: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.guid)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加商品管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const guid = row.guid || this.ids
        getProductinfo(guid).then(response => {
          const data = response.data;
          data.productflag = data.productflag.split(',').map(item => {
            return parseInt(item);
          });
          this.form = data;

          this.open = true;
          this.title = "修改商品管理";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.form.productflag = this.form.productflag.join(',');
            if (this.form.guid != null) {
              updateProductinfo(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addProductinfo(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const guids = row.guid || this.ids;
        this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProductinfo(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有商品管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProductinfo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
      }
    }
  };
</script>
